﻿@{
    ViewData["Title"] = "Update";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <script src="~/libs/jquery/jquery.js"></script>
    <link href="~/layui-v2.9.16/layui/css/layui.css" rel="stylesheet" />
    <script src="~/layui-v2.9.16/layui/layui.js"></script>

</head>
<body>
    <form class="layui-form" action="">

        <div class="layui-form" lay-filter="filter-test-layer" style="margin: 16px;">
            <div class="demo-login-container">

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">移库单号</label>
                    <div class="layui-input-wrap layui-input-inline">
                        <input type="text" name="TransferCode" value="" lay-verify="TransferCode" placeholder="移库单号" lay-reqtext="请填写入移库单号" autocomplete="off" class="layui-input" lay-affix="clear" id="TransferCode">
                    </div>
                    <div class="layui-input-wrap layui-input-inline">
                        <input type="checkbox" lay-skin="switch" lay-filter="switchTest" title="自动生成">
                    </div>

                </div> <br />

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">移库单类型</label>
                    <div class="layui-input-wrap layui-input-inline">
                        <select name="TransferType" lay-reqtext="请填写入客户编号" lay-filter="TransferType" id="TransferType">
                            <option value="0">请选择</option>
                            <option value="移库上架">移库上架</option>
                            <option value="仓库移库">仓库移库</option>
                            <option value="报损移库">报损移库</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">制单人</label>
                    <div class="layui-input-wrap layui-input-inline">
                        <input type="text" name="Maker" value="" id="Maker" lay-verify="Maker" placeholder="制单人" lay-reqtext="请填写入制单人" autocomplete="off" class="layui-input" lay-affix="clear">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">制单时间</label>
                    <div class="layui-input-wrap layui-input-inline">
                        <div class="layui-input-prefix">
                            <i class="layui-icon layui-icon-date"></i>
                        </div>
                        <input type="date" name="MakeTime" id="MakeTime" lay-verify="MakeTime" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">数量</label>
                    <div class="layui-input-wrap layui-input-inline">
                        <input type="number" name="Quantity" placeholder="" value="1" autocomplete="off" class="layui-input" min="1" step="1" lay-affix="number" id="Quantity">
                    </div>
                </div>


                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-wrap layui-input-inline ">
                        <textarea placeholder="请输入备注" name="Remark" id="Remark" value="" class="layui-textarea" lay-verify="Remark"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-addlogin">添加</button>
                </div>
            </div>
        </div>

    </form>

    <p border></p>

    <table class="layui-hide" lay-filter="test" id="ID-table-demo-page"></table>
    <br />

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
          <button  class="layui-btn layui-btn-sm4 layui-bg-red" lay-event="demoAdd">选择</button>
          </div>
    </script>

    <script type="text/html" id="toolDemo">
        <div class="layui-clear-space">
          <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
        </div>
    </script>

    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    <script>
        layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function () {
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var util = layui.util;
            var layer = layui.layer;
            var util = layui.util;
            var table = layui.table;
            var laypage = layui.laypage;

            var ids = [];
            var transeferList = JSON.parse(localStorage.getItem("TransferList"))
            console.log(transeferList)
            var dataFind = {
                Id: transeferList.id,
                TransferCode: transeferList.transferCode,
                TransferType: transeferList.transferType,
                Maker: transeferList.maker,
                MakeTime: transeferList.makeTime,
                Remark: transeferList.remark,
                Quantity: transeferList.quantity,
                idsString: "",
            }
            function transeferFind() {
                var makeTime = dataFind.MakeTime.split("T")[0];
                $("[name=TransferCode]").val(dataFind.TransferCode)
                $("[name=TransferType]").val(dataFind.TransferType)
                form.render("select")
                $("[name=Maker]").val(dataFind.Maker)
                $("[name=MakeTime]").val(makeTime)
                $("[name=Remark]").val(dataFind.Remark)
                $("[name=Quantity]").val(dataFind.Quantity)

            }
            //显示
            function GetWorkShop() {

                // 创建渲染实例
                table.render({
                    elem: '#ID-table-demo-page',
                    toolbar: '#toolbarDemo',
                    url: '/SalesController1/GetProducts', // 此处为静态模拟数据，实际使用时需换成真实接口
                    // css: [
                    //     // 对开启了编辑的单元格追加样式
                    //     '.layui-table-view td[data-edit]{color: #16B777;}'
                    // ].join(''),
                    cols: [[
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'productName', width: 150, title: '产品名称', sort: true },
                        { field: 'productCode', width: 150, title: '产品编码' },
                        { field: 'productSpecification', width: 150, title: '规则', sort: true },
                        { field: 'unit', width: 150, title: '单位', sort: true },
                        { field: 'unitPrice', width: 150, title: '单价', sort: true },
                        // {
                        //     field: 'number', width: 150, title: '数量', sort: true,
                        //     templet: function (row) {
                        //         return '<input type="number" class="layui-input" value="' + row.number + '" onchange="updateQuantity(this, \'' + row.numbers + '\')">';
                        //     }
                        // },
                        // { field: 'UnitPrice', width: 150, title: '总价', sort: true },
                        { field: 'right', width: 180, title: '操作', templet: '#toolDemo' }
                    ]],


                    parseData: function (res) {
                        return {
                            "code": 0, //解析接口状态
                            "data": res//解析数据列表
                        };
                    },
                });

            }



            GetWorkShop();
            transeferFind();
            var ProductCodes = "";

            // 工具栏事件
            table.on('toolbar(test)', function (obj) {
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);

                console.log(checkStatus)
                if (checkStatus.data.length == 0) {
                    layer.msg("请选择数据")

                }
                else {
                    switch (obj.event) {
                        case 'demoAdd':
                            var data = checkStatus.data;
                            console.log(data)
                            ids.length = 0;
                            for (var i = 0; i < data.length; i++) {
                                console.log(data[i])
                                ids.push({ id: data[i].productCode, num: data[i].num })
                            }
                            console.log(ids)

                            break;

                    };
                }

            });

            //日期
            laydate.render({
                elem: '#DeliveryDate'
            });
            laydate.render({
                elem: '#CreateTime'
            });


            //添加
            form.on('submit(demo-addlogin)', function (data, index) {
                if (ids.length == 0) {
                    layer.msg("请选择数据")
                }
                else {
                    var field = data.field; // 获取表单字段值
                    var worklist = field;
                    console.log(worklist)
                    var idsString = JSON.stringify(ids);
                    dataFind.TransferCode= worklist.TransferCode,
                    dataFind.TransferType= worklist.TransferType,
                    dataFind.Maker= worklist.Maker,
                    dataFind.MakeTime= worklist.MakeTime,
                    dataFind.Remark= worklist.Remark,
                    dataFind.Quantity= worklist.Quantity,
                    dataFind.idsString= idsString,
                    $.get("/TransferController1/UpdateTransfer", dataFind,
                        res => {
                            console.log(res);
                            if (res != null) {
                                console.log(res);
                                layer.msg('修改成功');
                                window.parent.location.reload();
                            }
                            else {
                                layer.msg('修改失败！', {

                                });
                            }
                        }
                    )
                }

                return false;
            });


            //表单操作
            table.on('tool(test)', function (obj) {
                var data = obj.data; // 获得当前行数据
                var listwork = data;

                localStorage.setItem("workshopId", listwork.Id);


                if (obj.event === 'del') {

                    layer.confirm('确定要删除吗？', function (index) {
                        $.ajax({
                            url: '/XwdManagement/DelProductAhow',
                            type: 'Get',
                            data: { Id: data.Id },
                            dataType: 'json',
                            success: function (res) {
                                layer.msg("删除成功");
                                GetWorkShop();
                            }
                        })
                        layer.close(index);
                        // 向服务端发送删除指令
                    });

                }

            });

            //生成
            var workshopCode = generateRandomNumber();
            form.on('switch(switchTest)', function (data) {
                var elem = data.elem; // 得到开关原始DOM对象
                var value = elem.checked; // 开关是否开启，true或者false
                console.log(value); // 输出开关状态
                var inputElem = document.getElementById('TransferCode');

                console.log(inputElem); // 输出input元素
                if (value == true) {
                    console.log(workshopCode); // 输出编码
                    inputElem.value = generateRandomNumber();// 根据开关状态给input赋值
                }
                else {
                    inputElem.value = " ";
                    console.log("关闭"); // 输出关闭
                }
            })
            //随机数
            function generateRandomNumber() {
                var num = Math.floor(10000000 + Math.random() * 90000000); // 生成1000到9999之间的随机数
                var number = "YK" + num
                return number;
            }


        });
    </script>

</body>
</html>



